import { getPaymentParamsAPI } from '@/api/pay'

export default {
  methods: {
    goToPay(order, openid, onSuccess, onError) {

      getPaymentParamsAPI(order.type, openid, order.out_trade_no).then(({ data }) => {
        const payPramas = data.payment_arguments
        // 调起微信支付
        this.$weixin.ready(() => {
          this.$weixin.chooseWXPay({
            timestamp: payPramas.timeStamp,
            nonceStr: payPramas.nonceStr,
            package: payPramas.package,
            signType: payPramas.signType,
            paySign: payPramas.paySign,
            success: res => {
              onSuccess && onSuccess(res)
            },
            fail: error => {
              onError && onError(error)
              console.log(error)
            },
            cancel: res => {
              onError && onError(res)
              console.log(res)
            }
          })
        })
      }).catch(error => {
        if (error.response) {
          const errorMsg = error.response.data.msg

          this.$vux.alert.show({
            type: 'warn',
            title: this.$t('failed'),
            content: errorMsg,
            buttonText: this.$t('known')
          })
        }

        onError && onError(error)
      })
    }
  }
}
